<%@page import="java.lang.reflect.Array"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*, myclasses.*, java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Country of citizenship</title>
</head>
<body>
<%	String firstName = request.getParameter("firstName");
	String middleInitial = request.getParameter("middleInitial");
	String lastName = request.getParameter("lastName");
	
	Student stud = new Student(firstName, middleInitial, lastName);
	session.setAttribute("student", stud);
%>
	
<center>
Welcome <%= firstName %> <%= middleInitial %>. <%= lastName %>.<br>
Please choose your country of citizenship:
</center>

<%  	
	try {
		Class.forName("org.postgresql.Driver");
	} catch(ClassNotFoundException e) {
		out.println("org.postgresql.Driver Exception. ");
	}

	try {
		String serverName = "localhost:5432";
	    String mydatabase = "ThirdBase";
	    String url = "jdbc:postgresql://" + serverName + "/" + mydatabase; 
	    String username = "SweetAss";
	    String password = "1234";
	    Connection conn = DriverManager.getConnection(url, username, password);
	    
		Statement stmt = conn.createStatement();

		ResultSet rset = stmt.executeQuery("SELECT id,name FROM country");
		
		//	====
		Integer columncounter = 0;
		out.println("<br>");
		out.println("<table width=\"600\" border=\"2\" cellpadding=\"2\" cellspacing=\"2\" align=\"center\">");
	
		while(rset.next()) {
			//Making sure it's 3 columns
			columncounter += 1;
			if(columncounter == 1)
			out.println("<tr>");
			out.println("<td>" + "<a href=\"http://localhost:8080/GradSchoolApp/residence.jsp?citizenship=" + rset.getString(1) + "\">" + rset.getString(2) + "</a>" + "</td>");
		
			if (columncounter >= 3) {
				out.println("</tr>");
				columncounter = 0;
			}	
		}

		out.println("</table>");
		out.println("<br>");	
		
		rset.close();
		stmt.close();
	    conn.close();
	    
	} catch(SQLException e) {
		e.printStackTrace();
		out.println("Connection exception.");
	}
%>
</body>
</html>